import axios from "axios";

//创建实例便于管理
const http = axios.create({
    baseURL: "http://localhost:3000/api",
    timeout: 1000,
    headers: { "X-Custom-Header": "foobar" }, //很少用到
});

async function get(url, params = {}) {
    const res = await http({
        url,
        method: "get",
        params,
    });
    return res;
}

async function post(url, data) {
    const res = await http({
        url,
        method: "post",
        data,
    });
    return res;
}

async function put(url, params = {}) {
    const res = await http({
        url,
        method: "put",
        params,
    });
    return res;
}

async function remove(url, params = {}) {
    const res = await http({
        url,
        method: "delete",
        params,
    });
    return res;
}

function get2(url, params = {}) {
    return new Promise((resolve, reject) => {
        http({
            url,
            method: "get",
            params,
        })
            .then((res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err);
            });
    });
}

export { get, post };